package com.fish.yssb.app;

import android.app.Activity;
import android.app.Application;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.fish.yssb.Consts;
import com.fish.yssb.data.model.LoginUser;
import com.fish.yssb.db.RealmDb;
import com.fish.yssb.db.bean.BaseInfoTable;
import com.fish.yssb.db.excel.ReadExcel;
import com.fish.yssb.ui.page.util.PrefeneceUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import io.realm.Realm;


public class BaseApp extends Application {

    public static BaseApp mInstance;

    public static LoginUser user;


    private String initData = "2";


    public static Activity activityRef;





    @Override
    public void onCreate() {
//        UMConfigure.init(this, "5cfdeb894ca357bc94000522", "5cfdeb894ca357bc94000522", UMConfigure.DEVICE_TYPE_PHONE, null);
//        UMConfigure.setLogEnabled(true);
        super.onCreate();
        mInstance = this;
        init();
        user = new LoginUser();


        RealmDb.getInstance().initRealm(this);
        PrefeneceUtil.getInstance().initPrefenece(this);
        initData();
        getUser();
    }

    private void initData()
    {
       String tmp =  PrefeneceUtil.getInstance().getData(Consts.Prefence_initData,"",false);


//        ReadExcel.getData(getApplicationContext());
        //判断是不是已经添加过初始值
        if(!tmp.equalsIgnoreCase(initData))
        {
//            addInitData();
            ReadExcel.getData(getApplicationContext());
        }
    }

    private void addInitData()
    {
        Log.e("=========","count:start");


        ArrayList<BaseInfoTable> list = new ArrayList<>();
        RealmDb.getInstance().deleteAllRealmObjects();


        RealmDb.getInstance().insertRealmObjects(list);
        List<BaseInfoTable> arrayList = (List<BaseInfoTable>) RealmDb.getInstance().queryRealmObjects(BaseInfoTable.class);

        Log.e("=========","count:"+arrayList.size());
        PrefeneceUtil.getInstance().setData(Consts.Prefence_initData,initData,false);
    }

    /*
     * 初始化参数
     */
    private void init() {

        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {
//                MyActivityManager.getInstance().setCurrentActivity(activity);
                activityRef =  activity;
            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });


    }


    public static BaseApp getInstance() {
        return mInstance;
    }

    public static void saveUser()
    {
        Gson gson = new Gson();
        String json = gson.toJson(user).toString();
        PrefeneceUtil.getInstance().setData(Consts.Prefence_User_info,json,false);

    }

    public static void getUser()
    {
        String json =  PrefeneceUtil.getInstance().getData(Consts.Prefence_User_info,"",false);
        Gson gson = new Gson();
        Type type = new TypeToken<LoginUser>() {
        }.getType();
        LoginUser tmp = gson.fromJson(json, type);
        if(tmp !=null)
        {
            user = tmp;
        }else{
            user = new LoginUser();
        }
//        Toast.makeText(PerfeneceActivity.this,arrayList.size()+"",Toast.LENGTH_SHORT).show();
    }


}
